<?php
/**
 * 名称：UserGroupModel
 * 描述：用户群组模型
 * 作者：lan 时间：2013-05-02
 * 版本号：V1.0.0
 */
class UserGroupModel extends Model {
	/**
	 * 添加用户组
	 * @param unknown_type $uids 用户ID
	 * @param unknown_type $gids 群组ID
	 * @return boolean
	 */
	public function addUserGroup($uids,$gids) {
		$gids = is_array ( $gids ) ? $gids : explode ( ',', $gids );
		$map['user_id'] = array('in',$uids);
		M('RoleUser')->where($map)->delete();
		unset($map);
		/* $ugroup = $this->getUserGroupById($gids);
		if(!$ugroup) {
			return false;
		} */
    	//组装SQL，插入新数据
    	if(!empty($gids)) {
	    	$sql = "INSERT INTO `" . C ( 'DB_PREFIX' ) . "role_user` (`role_id`,`user_id`) VALUES ";
	    	foreach($gids as $ggid) {
	    		$sql .= "('{$ggid}', '{$uids}'),";
	    	}
	    	$sql = rtrim($sql, ',');
	    	return $this->execute($sql);
    	}
	}
	
	/**
	 * 根据role_id查询信息
	 * @param unknown_type $gid 群组ID
	 * @return array
	 */
	public function getUserGroupById($gid) {
		$data['role_id'] = array('in',$gid);
		return M("RoleUser")->where($data)->select();
	}
	
	/**
	 * 验证用户组名称是否重复
	 * @param unknown_type $title 用户组名称
	 * @param unknown_type $uid 用户组ID
	 * @return array
	 */
	public function titleRepeat($title,$uid = 0) {
		$data ['user_group_id'] = array (
				'neq',
				$uid
		);
		$data['title'] = array('eq',$title);
		return $this->where($data)->find();
	}
	
	/**
	 * 验证积分介于是否重复
	 * @param unknown_type $max_credit
	 * @param unknown_type $uid
	 * @return array
	 */
	public function maxCrediteRepeat($max_credit,$uid = 0) {
		$data ['user_group_id'] = array (
				'neq',
				$uid
		);
		$data['max_credit'] = array('eq',$max_credit);
		return $this->where($data)->find();
	}
}

?>